package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageBox;
import com.moleq.models.AgeSetting;
import com.moleq.models.GlobalInfo;

public class AgeSettingService {
	private DatabaseHelper DBHelper;
	private Context mContext;
	private String errorString;

	public AgeSettingService() {

	}

	public AgeSettingService(Context context) {
		// this.DBHelper = new DatabaseHelper(context);
		mContext = context;
		this.DBHelper = new DatabaseHelper(
				GlobalInfo.getInstance().ShareContext);
	}

	public List<AgeSetting> FindAll() {
		List<AgeSetting> obj = new ArrayList<AgeSetting>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from t_age_setting", null);
		if (cursor == null) {
			MessageBox.show(mContext, DBError.error);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			int as_no = cursor.getInt(cursor
					.getColumnIndex(DatabaseSchema.AgeSetting.AS_NO));
			String as_desc = cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.AgeSetting.AS_DESC));
			int as_age = cursor.getInt(cursor
					.getColumnIndex(DatabaseSchema.AgeSetting.AS_AGE));

			obj.add(new AgeSetting(as_no, as_desc, as_age));
		}
		cursor.close();
		return obj;
	}

	public AgeSetting Find(int no) {

		AgeSetting obj = null;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(
				"select * from t_age_setting where as_no  = " + no, null);
		if (cursor == null) {
			MessageBox.show(mContext, DBError.error);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			int as_no = cursor.getInt(cursor
					.getColumnIndex(DatabaseSchema.AgeSetting.AS_NO));
			String as_desc = cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.AgeSetting.AS_DESC));
			int as_age = cursor.getInt(cursor
					.getColumnIndex(DatabaseSchema.AgeSetting.AS_AGE));
			String create_datetime = cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.AgeSetting.CREATE_TIME));
			String update_datetime = cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.AgeSetting.UPDATE_TIME));
			obj = new AgeSetting(as_no, as_desc, as_age, create_datetime,
					update_datetime);

		}
		cursor.close();
		return obj;

	}

	public int FindAge(int no) {
		int as_age = 0;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(
				"select as_age from t_age_setting where as_no  = " + no, null);
		if (cursor == null) {
			MessageBox.show(mContext, DBError.error);
			DBError.error = "";
			return 0;
		}
		while (cursor.moveToNext()) {
			as_age = cursor.getInt(cursor
					.getColumnIndex(DatabaseSchema.AgeSetting.AS_AGE));

		}
		cursor.close();
		return as_age;

	}

	public boolean Insert(AgeSetting obj) {
		boolean check = true;
		// SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "INSERT INTO t_age_setting values("
				+ obj.getAs_no()
				+ ",'"
				+ obj.getAs_desc()
				+ "',"
				+ obj.getAs_age()
				+ ",strftime('%Y%m%d%H%M%S','now'),strftime('%Y%m%d%H%M%S','now'))";
		errorString = DBHelper.execSQL(sql);

		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}

	public int FindMaxNo() {
		int as_no = 0;
		String sql = "Select Max(as_no) from "
				+ DatabaseSchema.AgeSetting.SCHEMA_NAME;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor == null) {
			MessageBox.show(mContext, DBError.error);
			DBError.error = "";
			return as_no;
		}
		while (cursor.moveToNext()) {
			as_no = cursor.getInt(0);
		}
		cursor.close();
		return as_no + 1;
	}

	public boolean Update(AgeSetting obj) {
		boolean check = true;
		// SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "Update t_age_setting set as_desc = '" + obj.getAs_desc()
				+ "', as_age = " + obj.getAs_age()
				+ ", update_datetime = strftime('%Y%m%d%H%M%S','now')"
				+ " where as_no = " + obj.getAs_no();
		errorString = DBHelper.execSQL(sql);

		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}

	public boolean CheckAge(String age, String name) {
		boolean lb_check = true;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "select as_no from t_age_setting where as_desc  = '"
				+ name + "'" + " and as_age = " + age;
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor == null) {
			MessageBox.show(mContext, DBError.error);
			DBError.error = "";
			return true;
		}
		if (cursor.moveToNext()) {

			lb_check = false;
		}
		cursor.close();
		return lb_check;
	}

	public boolean CheckAge(String age_no) {
		boolean lb_check = true;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "select as_no from t_age_setting where as_no  = " + age_no;
//		String sql2 = "select F01 from t_item_setting where is_age_ver = "
//				+ age_no;
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor == null) {
			MessageBox.show(mContext, DBError.error);
			DBError.error = "";
			return true;
		}
		if (cursor.moveToNext()) {

			lb_check = false;
		}
		cursor.close();
//		Cursor cursor2 = db.rawQuery(sql2, null);
//		if (cursor2 == null) {
//			MessageBox.show(mContext, DBError.error);
//			DBError.error = "";
//			return true;
//		}
//		if (cursor2.moveToNext()) {
//			cursor2.close();
//			lb_check = false;
//		}
//		cursor2.close();

		return lb_check;
	}

	public boolean CheckAgeByNO(String age_no) {
		boolean lb_check = true;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql2 = "select F01 from t_item_setting where is_age_ver = "
				+ age_no;
		Cursor cursor2 = db.rawQuery(sql2, null);
		if (cursor2 == null) {
			MessageBox.show(mContext, DBError.error);
			DBError.error = "";
			return true;
		}
		if (cursor2.moveToNext()) {
			cursor2.close();
			lb_check = false;
		}
		cursor2.close();

		return lb_check;
	}
	
	public int FindNo(String age, String name) {
		int no = 0;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "select as_no from t_age_setting where as_desc  = '"
				+ name + "'" + " and as_age = " + age;
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor == null) {
			// ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			MessageBox.show(mContext, DBError.error);
			DBError.error = "";
			return 0;
		}
		if (cursor.moveToNext()) {

			no = cursor.getInt(0);
		}
		cursor.close();
		return no;
	}

	public boolean CheckAgeByDepNo(int no) {
		boolean lb_check = true;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "select ds_age_ver from t_department_setting where ds_age_ver  = "
				+ no;
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor == null) {
			// ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			MessageBox.show(mContext, DBError.error);
			DBError.error = "";
			return true;
		}
		if (cursor.moveToNext()) {

			lb_check = false;
		}
		cursor.close();
		return lb_check;
	}
	
	public boolean CheckAgeBySubdepNo(int no) {
		boolean lb_check = true;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "select sd_age_ver from t_sub_dept_setting where sd_age_ver  = "
				+ no;
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor == null) {
			// ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			MessageBox.show(mContext, DBError.error);
			DBError.error = "";
			return true;
		}
		if (cursor.moveToNext()) {

			lb_check = false;
		}
		cursor.close();
		return lb_check;
	}

	public boolean DeleteAge(int age_no) {
		boolean check = true;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "Delete from t_age_setting ";
		String ls_where = "where as_no  = " + age_no;
		sql = sql + ls_where;
		try {
			db.execSQL(sql);
		} catch (Exception e) {
			check = false;
		}
		return check;
	}

	public int getMaxNo() {
		int maxAge = 0;
		try {
			SQLiteDatabase db = DBHelper.getWritableDatabase();
			String strSQL = "select Max(as_no) from t_age_setting";
			Cursor cursor = db.rawQuery(strSQL, null);

			if (cursor.moveToFirst()) {
				maxAge = cursor.getInt(0);
			}
			cursor.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return maxAge;
	}
	
}
